﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLyThuVien
{
    public partial class TraCuuSach : Form
    {
        public TraCuuSach()
        {
            InitializeComponent();
        }
        void LoadDSTheLoaiSach()
        {
            DataTable dt = LoaiSachBUS.LayTatCaTheLoai_BUS();
            combTheLoaiSach.DataSource = dt;
            combTheLoaiSach.DisplayMember = "TenLoaiSach";
            combTheLoaiSach.ValueMember = "MaTheLoaiSach";
            

        }
        void LoadDSSach()
        {
            DataTable dt = SachBUS.LayDanhSachSach_BUS();
            combMaSach.DataSource = dt;
            combMaSach.ValueMember = "MaSach";
            combMaSach.DisplayMember = "MaSach";
        }
        private void btlammoi_Click(object sender, EventArgs e)
        {

            foreach (Control ctrl in groupPanel1.Controls)
            {
                if (ctrl.GetType() != typeof(DevComponents.DotNetBar.ButtonX) && ctrl.GetType() != typeof(DevComponents.DotNetBar.LabelX))
                {
                    ctrl.ResetText();
                }
            }
        }

        private void bttracuu_Click(object sender, EventArgs e)
        {
            if (chkTKThuong.Checked)
            {
                DataTable dt = SachBUS.TimKiemSach(txtTenSach.Text);
                dataGridView1.DataSource = dt;
                dataGridView1.Columns["MaSach"].HeaderText = "Mã sách";
                dataGridView1.Columns["TenSach"].HeaderText = "Tên sách";
                dataGridView1.Columns["TenLoaiSach"].HeaderText = "Loại sách";
                dataGridView1.Columns["TacGia"].HeaderText = "Tác giả";
                dataGridView1.Columns["NamXuatBan"].HeaderText = "Năm xuất bản";
                dataGridView1.Columns["NhaXuatBan"].HeaderText = "Nhà xuất bản";
                dataGridView1.Columns["NgayNhap"].HeaderText = "Ngày nhập";
                dataGridView1.Columns["Gia"].HeaderText = "Giá";
                dataGridView1.Columns["HoTen"].HeaderText = "Họ tên";
                dataGridView1.Columns["TinhTrang"].HeaderText = "Tình trạng";
                dataGridView1.Columns["TheLoai"].Visible = false;
             
            }
            else if(chkTKNangCao.Checked)
            {
                string param = "";
                if (txtTenSach.Text != "")
                    param += " and Sach.TenSach='" + txtTenSach.Text + "' ";
                if (txtTacGia.Text.Length > 0)
                    param += " and Sach.TacGia like '%" + txtTacGia.Text + "%'";
                if (int.Parse(combTheLoaiSach.SelectedValue.ToString()) > -1)
                    param += " and Sach.TheLoai=" + combTheLoaiSach.SelectedValue.ToString() + " ";
                if (txtGia.Text != "")
                    param += " and Sach.Gia=" + txtGia.Text;
                if (txtNamXuatBan.Text != "")
                    param += " and Sach.NamXuatBan=" + txtNamXuatBan.Text;
                DataTable dt = SachBUS.TimKiemSachNangCao_BUS(param);
                dataGridView1.DataSource = dt;
                dataGridView1.Columns["MaSach"].HeaderText = "Mã sách";
                dataGridView1.Columns["TenSach"].HeaderText = "Tên sách";
                dataGridView1.Columns["TenLoaiSach"].HeaderText = "Loại sách";
                dataGridView1.Columns["TacGia"].HeaderText = "Tác giả";
                dataGridView1.Columns["NamXuatBan"].HeaderText = "Năm xuất bản";
                dataGridView1.Columns["NhaXuatBan"].HeaderText = "Nhà xuất bản";
                dataGridView1.Columns["NgayNhap"].HeaderText = "Ngày nhập";
                dataGridView1.Columns["Gia"].HeaderText = "Giá";
                dataGridView1.Columns["HoTen"].HeaderText = "Họ tên";
                dataGridView1.Columns["TinhTrang"].HeaderText = "Tình trạng";
                dataGridView1.Columns["TheLoai"].Visible = false;
            }
        }

        private void TraCuuSach_Load(object sender, EventArgs e)
        {
            LoadDSTheLoaiSach();
            LoadDSSach();
        }
        int maSach = -1;
        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0&&dataGridView1.SelectedRows[0].IsNewRow==false)
            {
                DataGridViewRow dr = dataGridView1.SelectedRows[0];
                txtNamXuatBan.Text = dr.Cells["NamXuatBan"].Value.ToString();
                txtNXB.Text = dr.Cells["NhaXuatBan"].Value.ToString();
                txtTacGia.Text = dr.Cells["TacGia"].Value.ToString();
                txtGia.Text = dr.Cells["Gia"].Value.ToString();
                combTheLoaiSach.SelectedValue=int.Parse(dr.Cells["TheLoai"].Value.ToString());
                txtTenSach.Text = dr.Cells["TenSach"].Value.ToString();
                maSach = int.Parse(dr.Cells["MaSach"].Value.ToString());
            }
        }

        private void btxoasanh_Click(object sender, EventArgs e)
        {
            if (maSach > -1)
            {
                SachBUS.XoaSach_BUS(maSach);
                bttracuu_Click(null, null);
            }
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btcapnhat_Click(object sender, EventArgs e)
        {
            SachDTO sachDTO = new SachDTO();
            sachDTO.Masach = maSach;
            sachDTO.Tensach = txtTenSach.Text;
            sachDTO.Gia = int.Parse(txtGia.Text);
            sachDTO.Namxuatban = int.Parse(txtNamXuatBan.Text);
            sachDTO.Ngaynhap = dtpFrom.Value;
            sachDTO.Nguoitiepnhan = NhanVienBUS.laydanhsachnhanvien_bus()[0].Manhanvien;
            sachDTO.Nhaxuatban = txtNXB.Text;
            sachDTO.Tacgia = txtTacGia.Text;
            sachDTO.Tinhtrang =int.Parse( TinhTrangBUS.LayTatCaTinhTrang_BUS().Rows[0]["MaTinhTrang"].ToString());
            sachDTO.Theloai = int.Parse(combTheLoaiSach.SelectedValue.ToString());
            SachBUS.CapNhatSach_BUS(sachDTO);
            txtTenSach.Text = "";
            bttracuu_Click(null, null);
            
        }

       
    }
}